package com.wmyg.antifake.utils;

import com.wmyg.antifake.bean.*;
import com.wmyg.antifake.dao.PictureMapper;
import com.wmyg.antifake.bean.QrProduct;
import com.wmyg.antifake.bean.Qrcode;
import com.wmyg.antifake.bean.TrackInfo;
import com.wmyg.antifake.dao.QrProductMapper;
import com.wmyg.antifake.dao.QrcodeMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
public class TrackInfoConverter {

    public static Map  convert(String qR, TrackInfo trackInfo, HttpServletRequest request, QrcodeMapper qrcodeMapper, QrProductMapper qrProductMapper,PictureMapper pictureMapper) {
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        LinkedHashMap qrMap = new LinkedHashMap();
        qrMap.put("times", trackInfo.getTimes() == 0?1:trackInfo.getTimes());
        log.info("This qr is queried {} times", qrMap.get("times"));
        qrMap.put("qrUrl", "https://minip.weimengyungu.cn/antifake/track/qr.do?qR=" + trackInfo.getQrInfo().getItemInfo().getQR());
        String s = trackInfo.getQrInfo().getItemInfo().getQR();
        qrMap.put("qrString", new String[]{s.substring(0,18), s.substring(18,28), s.substring(28)});
        qrMap.put("type", "QR_CODE");
        linkedHashMap.put("qrInfo", qrMap);
//        LinkedHashMap brandMap = new LinkedHashMap();
//        brandMap.put("id", "B" + trackInfo.getProduct().getBrandId());
//        String brandLogo = trackInfo.getProduct().getBrand().getBrandLogo();
//        if (!"".equals(brandLogo.trim()) && brandLogo != null) {
//            brandMap.put("logo", request.getHeader("host") + request.getContextPath() + brandLogo);
//        }
//        LinkedHashMap brandMapInside = new LinkedHashMap();
//        brandMapInside.put("品牌名称", trackInfo.getProduct().getBrand().getBrandName());
//        brandMapInside.put("品牌介绍", trackInfo.getProduct().getBrand().getBrandIntroduce());
//        brandMapInside.put("企业地址", trackInfo.getProduct().getBrand().getBrandAddress());
//        brandMap.put("brief", brandMapInside);
//        linkedHashMap.put("brand", brandMap);
//        LinkedHashMap factoryMap = new LinkedHashMap();
//        factoryMap.put("id", "F" + trackInfo.getProduct().getFactoryId());
//        Factory factory = trackInfo.getProduct().getFactory();
//        List<Picture> picList = pictureMapper.selectPic(1,String.valueOf(factory.getFactoryId()),1);
//        String factoryLogo = picList != null && picList.size() != 0 ? picList.get(0).getPicUrl() : null;
//        if (!"".equals(factoryLogo.trim()) && factoryLogo != null) {
//            factoryMap.put("logo", request.getHeader("host") + request.getContextPath() +  "/resource" +factoryLogo);
//        }
//        LinkedHashMap factoryMapInside = new LinkedHashMap();
//        factoryMapInside.put("厂商名称", trackInfo.getProduct().getFactory().getFactoryName());
//        factoryMapInside.put("厂商地址", trackInfo.getProduct().getFactory().getFactoryAddress());
//        factoryMapInside.put("厂商介绍", trackInfo.getProduct().getFactory().getFactoryIntroduce());
//        factoryMap.put("brief", factoryMapInside);
//        linkedHashMap.put("factory", factoryMap);
//        LinkedHashMap productMap = new LinkedHashMap();
//        productMap.put("id", "P" + trackInfo.getQrInfo().getItemInfo().getItemNo());
        //String productLogo = trackInfo.getProduct().getProductImg();
//        List<Picture> picList1 = pictureMapper.selectPic(3,String.valueOf(trackInfo.getProduct().getProductId()),3);
//        String productLogo = picList1 != null && picList1.size() != 0 ? picList1.get(0).getPicUrl() : null;
//        if (!"".equals(productLogo.trim()) && productLogo != null) {
//            productMap.put("logo", request.getHeader("host") + request.getContextPath() + "/resource" + productLogo);
//        }
//        LinkedHashMap productMapInside = new LinkedHashMap();
//        productMapInside.put("品牌", trackInfo.getProduct().getBrand().getBrandName());
//        //TODO 获取二维码类型
//        String qr_ = qR.substring(qR.indexOf("(21)")+4,qR.indexOf("(21)")+5);
//        if(qr_.equals("X") || qr_.equals("H")){
//            Qrcode qrCode = new Qrcode();
//            qrCode.setProductId(qR.substring(qR.indexOf("(01)0")+5,qR.indexOf("(10)")));
//            qrCode.setProductBatch(qR.substring(qR.indexOf("(10)")+4,qR.indexOf("(21)")));
//            Qrcode qrcode= qrcodeMapper.findProIdANdProBan(qrCode);
//            if(qr_.equals("X")){
//                //TODO 获取对应的盒数量
//                trackInfo.getProduct().setProductStandard(qrcode.getBoxNum()+"盒");
//                trackInfo.getProduct().setProductName(trackInfo.getProduct().getProductName()+"(箱)");
//            }else if(qr_.equals("H")){
//                //TODO 获取对应的包数量*克数
//                trackInfo.getProduct().setProductStandard(qrcode.getPackageNum()+"*"+qrcode.getProductBatch());
//                trackInfo.getProduct().setProductName(trackInfo.getProduct().getProductName()+"(盒)");
//            }
//        }else if(qr_.equals("D")){
//            //TODO 获取对应的克数
//            trackInfo.getProduct().setProductName(trackInfo.getProduct().getProductName()+"(包)");
//        }
//        String enterBatch = trackInfo.getQrInfo().getItemInfo().getEnterBatch();
//        QrProduct qrProduct = qrProductMapper.selectQrProductByQrCodeNumber(enterBatch);
//        productMapInside.put("产品名称", trackInfo.getProduct().getProductName());
////        productMapInside.put("产品规格", trackInfo.getProduct().getProductStandard());
//        productMapInside.put("生产日期", qrProduct!=null && StringUtils.isNotEmpty(qrProduct.getProductCreatTime().toString())?new SimpleDateFormat("yyyy-MM-dd").format(qrProduct.getProductCreatTime()):trackInfo.getQrInfo().getItemInfo().getProductTime().substring(0,trackInfo.getQrInfo().getItemInfo().getProductTime().indexOf(" ")));
//        productMapInside.put("保质期至", qrProduct!=null && StringUtils.isNotEmpty(qrProduct.getProductQualityTime().toString())?new SimpleDateFormat("yyyy-MM-dd").format(qrProduct.getProductQualityTime()):trackInfo.getQrInfo().getItemInfo().getProductTime().substring(0,trackInfo.getQrInfo().getItemInfo().getProductTime().indexOf(" ")));
//        productMapInside.put("生产批次", qrProduct!=null && StringUtils.isNotEmpty(qrProduct.getProductBatch())?qrProduct.getProductBatch():enterBatch);
//        productMap.put("brief", productMapInside);
//        linkedHashMap.put("product", productMap);
        return linkedHashMap;
    }

}
